import { getSecond, uuid } from '../../utils/common'
import { _sendVerificationCode , _checkVerificationCode } from '../../utils/sendCode'
import { ManageAdmin , Account , AccountDetail} from '../../models/'
import { CODE , SEND_TIME , TOKEN_TIME , VERIFICATION_CODE_TYPE , UID_KEY , _getCountLimit} from '../../config/config'
import { _getAccount , _bindPhone } from '../../utils/account' 

/**
 * @api {post} /manage_user/userList 获取用户列表
 * @apiPermission 管理员
 * @apiVersion 1.0.0 
 * @apiName userList
 * @apiGroup z_manage_user
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/manage_user/userList'

 * @apiParam {String}  token                   管理员token
 * @apiParam {Integer}  page                   页码 1开始
 * @apiParam {Integer}  limit                  每页显示条数 默认50
 *
 *
 * @apiSuccess {Integer}  count                总条数
 * @apiSuccess {Array}    accounts             用户信息集合
 * @apiSuccess {Object}   account              用户信息
 * 
 * @apiSuccess {String}   uid                  用户id
 * @apiSuccess {String}   phone                手机
 * @apiSuccess {String}   qq_key               qqkey
 * @apiSuccess {String}   wechat_key           微信key
 * @apiSuccess {String}   remark               备注
 * @apiSuccess {String}   ext                  拓展字段
 * @apiSuccess {String}   ctime                创建时间
 * @apiSampleRequest http://copyright.hotchain.vip/api/manage_user/userList
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 */
export async function userList(ctx) {
    let page  	           = ctx.request.body.page
	if(!page) {
		page               = 0
	}
	page                   = Number(page) - 1
	const countLimit       = _getCountLimit(ctx.request.body.limit)
	const where            = {status : 1}
	const order			   = [['ctime', 'DESC']]
	const limit			   = [page * countLimit,countLimit]
	const query            = {where,order,limit}

	const accounts         = await Account.findAll(query)
	const count            = await Account.count({where})

	ctx.body               = CODE.success({
		lit:accounts,count
	})
}

/**
 * @api {post} /manage_user/userDetail 获取用户详情
 * @apiPermission 管理员
 * @apiVersion 1.0.0 
 * @apiName userDetail
 * @apiGroup z_manage_user
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/manage_user/userDetail'

 * @apiParam {String}  token                   管理员token
 * @apiParam {Integer}  uid                    用户id
 *
 * @apiSuccess {Object}   accountDetail        用户详细信息
 * 
 * @apiSuccess {String}   uid                  用户id
 * @apiSuccess {String}   icon                 头像
 * @apiSuccess {String}   phone                手机
 * @apiSuccess {String}   nickname             昵称
 * @apiSuccess {String}   personal_id          个人认证id
 * @apiSuccess {String}   address_id           地址管联id
 * @apiSuccess {String}   company_id           企业认证id
 * @apiSuccess {String}   remark               备注
 * @apiSuccess {String}   ext                  拓展字段
 * @apiSuccess {String}   ctime                创建时间
 * @apiSampleRequest http://copyright.hotchain.vip/api/manage_user/userDetail
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 */
export async function userDetail(ctx) {
	const uid               = ctx.request.body.uid
	const accountDetail     = await AccountDetail.findOne({where : {uid,status:1}})
	ctx.body                = CODE.success({accountDetail})
}

export async function checkToken(ctx,next) {
	const token  = ctx.request.body.token
	const admin  = await ManageAdmin.findOne({where:{uid:token,status:1}})
	if(admin) {
		ctx.body = { admin }
		if(next) {
			return next()
		}
	}
	ctx.body     = CODE.MANAGE_ERROR.ERROR_ADMIN_NOT_FOUND
}
/**
 * @apiDefine ERROR
 * @apiErrorExample {json} 错误返回:
 *     {
 *       "code": "错误码",
 *       "error": "错误描述",
 *       "msg":"错误信息"
 *     }
 */